﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Oca35
{
    public class Board
    {
        public List <string> ActionPlayer{get;set;}

        public Player CurrentPlayer { get; set; }

        public Player[] Players { get; set; }

        public Dice Dice { get; set; }

        public Square[] Squares { get; set; }

        public ILogger Logger
        {
            get;
            set;
        }
        

        public Board()
        {
            Logger = new Logger();
            Dice = new Dice();
            BuildBoard();
        }

        private void BuildBoard()
        {
            Squares = new Square[42];
            Squares[0] = new Square { Board = this, Index = 0 };
            Squares[1] = new Square { Board = this, Index = 1 };
            Squares[2] = new Square { Board = this, Index = 2 };
            Squares[3] = new Square { Board = this, Index = 3 };
            Squares[4] = new OcaSquare { Board = this, Index = 4 };
            Squares[5] = new BridgeSquare { Board = this, Index = 5 };
            Squares[6] = new Square { Board = this, Index = 6 };
            Squares[7] = new Square { Board = this, Index = 7 };
            Squares[8] = new OcaSquare { Board = this, Index = 8 };
            Squares[9] = new Square { Board = this, Index = 9 };
            Squares[10] = new MazeSquare { Board = this, Index = 10 };
            Squares[11] = new BridgeSquare { Board = this, Index = 11 };
            Squares[12] = new Square { Board = this, Index = 12 };
            Squares[13] = new OcaSquare { Board = this, Index = 13 };
            Squares[14] = new Square { Board = this, Index = 14 };
            Squares[15] = new Square { Board = this, Index = 15 };
            Squares[16] = new MazeSquare { Board = this, Index = 16 };
            Squares[17] = new OcaSquare { Board = this, Index = 17 };
            Squares[18] = new Square { Board = this, Index = 18 };
            Squares[19] = new Square { Board = this, Index = 19 };
            Squares[20] = new MazeSquare { Board = this, Index =20 };
            Squares[21] = new JailSquare { Board = this, Index = 21 };
            Squares[22] = new OcaSquare { Board = this, Index = 22 };
            Squares[23] = new Square { Board = this, Index = 23 };
            Squares[24] = new PitSquare { Board = this, Index = 24 };
            Squares[25] = new Square { Board = this, Index = 25 };
            Squares[26] = new OcaSquare { Board = this, Index = 26 };
            Squares[27] = new PitSquare { Board = this, Index =27 };
            Squares[28] = new Square { Board = this, Index = 28 };
            Squares[29] = new JailSquare { Board = this, Index = 29 };
            Squares[30] = new PitSquare { Board = this, Index = 30 };
            Squares[31] = new OcaSquare { Board = this, Index = 31 };
            Squares[32] = new Square { Board = this, Index = 32 };
            Squares[33] = new Square { Board = this, Index = 33 };
            Squares[34] = new Square { Board = this, Index = 34 };
            Squares[35] = new OcaSquare { Board = this, Index = 35 };
            Squares[36] = new Square { Board = this, Index = 36 };
            Squares[37] = new PitSquare { Board = this, Index = 37 };
            Squares[38] = new JailSquare { Board = this, Index = 38 };
            Squares[39] = new Square { Board = this, Index = 39 };
            Squares[40] = new OcaSquare { Board = this, Index = 40 };
            Squares[41] = new Square { Board = this, Index = 41 };
        }

        public void PlayMyTurn(Player player)
        {
            var square = Squares[player.Location.Index + Dice.Draw()];
            player.Location = square;
            square.ApplyActions(player);
        }

        public void SetNewLocation(Player player, int Index)
        {
            player.Location = Squares[Index];
        }

        public void StartGame()
        {
            Logger.Write("El Juego ha comenzado. Haga Click en Sortear");
            foreach (var player in Players)
            {
                SetNewLocation(player,0);       
            }
            CurrentPlayer = Players[0];
        }

        public bool NextTurn()
        {
            if (CurrentPlayer.Index < Players.Length-1)
            {
                CurrentPlayer = this.Players[CurrentPlayer.Index + 1];
            }
            else
            {
                CurrentPlayer = Players[0];
            }

            foreach (var player in Players)
            {
                if (player.Location.Index == 41)
                {
                    Logger.Write("El juego TERMINO! Ganó: " + player.Name);
                    return false;
                }
            }
            Logger.Write("Es el turno de " + CurrentPlayer.Name);
            return true;
        }
    }
}
